package com.yanyu.space.sys.service.test.impl;

import com.yanyu.space.common.log.handler.LogFactory;
import com.yanyu.space.common.log.handler.LogWorkerContext;
import com.yanyu.space.common.log.support.LogRunnable;
import com.yanyu.space.member.feign.IMemberClient;
import com.yanyu.space.sys.service.test.ISleuthService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.cloud.sleuth.annotation.ContinueSpan;
import org.springframework.cloud.sleuth.annotation.NewSpan;
import org.springframework.cloud.sleuth.annotation.SpanTag;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gz
 */
@Service
@Slf4j
public class SleuthServiceImpl implements ISleuthService {
    @Autowired
    private IMemberClient memberClient;
    @Autowired
    private Tracer tracer;

    @Override
    @Async
    public Boolean execute(Integer num) {
        log.info("线程：" + Thread.currentThread().getName() + " , 任务：" + num);
//        try {
//            memberClient.test("tttgz");
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        log.info("线程：完成");

        return true;
    }

    @SneakyThrows
    @Override
    public boolean sech() {
        memberClient.testRequestParam("tttgz1111");
        Span span = tracer.nextSpan().event("cs").name("sech");
        new Thread(() -> {
            try (Tracer.SpanInScope spanInScope = tracer.withSpan(span.start());
                 LogWorkerContext logWorkerContext = LogFactory.getInstance().start(span.context())) {
                try {
                    memberClient.testRequestParam("ttt56");
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("seache");
            } finally {
                span.end();
            }
        }).start();

        Map<String,String> tags = new HashMap<>();
        tags.put("test1", "test1");
        tags.put("test2", "test2");
        tags.put("test3", "test3");
        new Thread(new LogRunnable(tags) {
            @Override
            protected void execute() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("seache1111");
            }
        }).start();
        return false;
    }

    @Override
    @NewSpan(name = "gz1New")
    public boolean annoNewSpan() {
        log.info("执行成功");
        return false;
    }

    @Override
    @ContinueSpan(log = "gz1Continue")
    public boolean annoContinueSpan() {
        log.info("执行成功");
        return false;
    }

    @Override
    @NewSpan(name = "gz1NewSpanTag")
    public boolean annoNewSpanTag(@SpanTag(key = "zhangsan1", expression = "test") String test) {
        log.info("执行成功");
        return false;
    }
    @Override
    @ContinueSpan(log = "gz1ContinueSpanTag")
    public boolean annoContinueSpanTag(@SpanTag(key = "zhangsan2", expression = "test") String test) {
        log.info("执行成功");
        return false;
    }
}
